package com.intro.action;
import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

 

@Entity
public class FileUploadAction extends ActionSupport  {

    @Id
	@GeneratedValue
	private static final long serialVersionUID = 572146812454l ;

    private static final int BUFFER_SIZE = 16 * 1024 ;

    private File myFile;         //网页中file字段的name

    private String contentType;  //struts2中必须字段,主要是set方法

    private String fileName;     // struts2中必须字段,主要是set方法

    private String imageFileName;   //要上传的图片在服务器中的名称

    private String imagePath;       //保存服务器中的路径

    private String pagePath;        //页面中要引用的url

   private HttpServletResponse response;

    public String getPagePath() {

       return pagePath;

    }

 

    public void setPagePath(String pagePath) {

       this.pagePath = pagePath;

    }

 

    public String getImagePath() {

       return imagePath;

    }

 

    public void setImagePath(String imagePath) {

       this.imagePath = imagePath;

    }

 

    public void setMyFileContentType(String contentType)  {

        this.contentType = contentType;

   }

  

    public void setMyFileFileName(String fileName)  {

        this.fileName = fileName;

   }

    public void setMyFile(File myFile)  {

        this .myFile = myFile;

   }

  

    public String getImageFileName()  {

        return imageFileName;

   }

    private static void copy(File src, File dst)  {

        try  {

           InputStream in = null ;

           OutputStream out = null ;

            try  {               

               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);

               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);

                byte [] buffer = new byte [BUFFER_SIZE];

                while (in.read(buffer) > 0 )  {

                   out.write(buffer);

               }

            } finally  {

                if ( null != in)  {

                   in.close();

               }

                 if ( null != out)  {

                   out.close();

               }

           }

        } catch (Exception e)  {

           e.printStackTrace();

       }

   }

 

   @Override

    public String execute()      {     

       imageFileName = new Date().getTime() +"_"+fileName;   //此名称用当前时间与上传图片名组成
       																	//ckeditor/images/Image
      imagePath = ServletActionContext.getServletContext().getRealPath( "uploadFiles\\images" ) + "\\" + imageFileName;

       System.out.println(imagePath);

       pagePath = getWebPath()+"uploadFiles\\images" + "\\" + imageFileName; //页面引用位置

       System.out.println("pagePath:"+pagePath);
       if(pagePath!=null) {
    	   //response.set
       }
       
       File imageFile = new File(imagePath);
       
       copy(myFile, imageFile);

       return SUCCESS;

   }

    private String getWebPath(){

       HttpServletRequest request = ServletActionContext.getRequest ();

      

       String path = request.getContextPath();

       String basePath = request.getScheme() + "://"

              + request.getServerName() + ":" + request.getServerPort()

              + path + "/";

       return basePath;

    }
    


}